﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Web.UserControls;
using StoragePeriod.Data;
using System.Data;
using System.Text;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;
using StoragePeriod.Web.NopiExcel;

namespace StoragePeriod.Web.FoodTesting
{
    /// <summary>
    /// 检测数据查询
    /// </summary>
    public partial class FoodSearchList : SecurityPage
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        T_ScoreData t_ScoreData = new T_ScoreData();

        protected void Page_Load(object sender, EventArgs e)
        {
            LeftTree.SelectedChanging += new MaterialTree.TreeEventDelegate(LeftTree_SelectedChanging);
            LeftLable.Text = "数据查询";
        }

        //树形菜单选择事件
        protected void LeftTree_SelectedChanging(Dictionary<string, string> dic)
        {
            DataTable RawListDt=this.InitRawListDt();
            this.HidRawList.Value="";
            if (dic.Count > 0)
            {
                StringBuilder sbRawList = new StringBuilder();
                int index = 0;
                dic.ToList().ForEach(p =>
                {
                    sbRawList.Append(p.Key+"|"+p.Value+",");
                    index += 1;
                    DataRow row = RawListDt.NewRow();
                    row["ID"] = index;
                    row["RawName"] = p.Value;
                    row["FoodTime"] = "";
                    row["SenseScore"] = "";
                    row["VCFood"] = "";
                    row["WaterFood"] = "";
                    row["CarbohydratesFood"] = "";
                    row["ProteinFood"] = "";
                    row["MineralFood"] = "";
                    row["VAFood"] = "";
                    row["VEFood"] = "";
                    row["AminoAcidFood"] = ""; //氨基酸
                    row["DietaryFiberFood"] = "";//膳食纤维
                    row["CuFood"] = "";
                    row["FeFood"] = "";
                    row["ZnFood"] = "";
                    row["CaFood"] = "";
                    row["MgFood"] = "";
                    row["PFood"] = "";
                    RawListDt.Rows.Add(row);
                });
                this.HidRawList.Value = sbRawList.ToString().TrimEnd(',');
            }
            this.RepList.DataSource = RawListDt;
            this.RepList.DataBind();
        }

        //初始化原料检测数据表
        private DataTable InitRawListDt()
        {
            DataTable RawListDt = new DataTable();
            RawListDt.Columns.Add("ID");            //序号
            RawListDt.Columns.Add("RawName");       //原料名称
            RawListDt.Columns.Add("FoodTime");      //检测时间
            RawListDt.Columns.Add("SenseScore");    //感官评分
            RawListDt.Columns.Add("VCFood");       //Vc含量
            RawListDt.Columns.Add("WaterFood");     //水分含量
            RawListDt.Columns.Add("CarbohydratesFood"); //碳水化合物
            RawListDt.Columns.Add("ProteinFood"); //蛋白质
            RawListDt.Columns.Add("MineralFood"); //矿物质
            RawListDt.Columns.Add("VAFood");
            RawListDt.Columns.Add("VEFood");
            RawListDt.Columns.Add("AminoAcidFood"); //氨基酸
            RawListDt.Columns.Add("DietaryFiberFood");//膳食纤维
            RawListDt.Columns.Add("CuFood");
            RawListDt.Columns.Add("FeFood");
            RawListDt.Columns.Add("ZnFood");
            RawListDt.Columns.Add("CaFood");
            RawListDt.Columns.Add("MgFood");
            RawListDt.Columns.Add("PFood");
            return RawListDt;
        }

        //根据时间间隔进行查询
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            string strRawIDList = this.HidRawList.Value;
            if (strRawIDList == "")
            {
                ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", "<script>alert('请选择原料!');</script>");
                return;
            }
            this.RepList.DataSource = this.GetFoodDt(strRawIDList);
            this.RepList.DataBind();
        }

        //获取检测数据列表
        private DataTable GetFoodDt(string strRawIDList)
        {
            DataTable RawListDt = this.InitRawListDt();
            string[] RawList = strRawIDList.Split(',');
            DateTime startTime = Convert.ToDateTime(this.txtStartTime.Text);
            DateTime endTime = Convert.ToDateTime(this.txtEndTime.Text);
            int Index = 1; //序号
            for (int i = 0; i < RawList.Length; i++)
            {
                int RawID = int.Parse(RawList[i].Split('|')[0]);
                string RAWName = RawList[i].Split('|')[1];

                for (DateTime time = startTime; time <= endTime; time = time.AddDays(1))
                {
                    bool IsAdd = false; //是否添加此行

                    DataRow rowTemp = RawListDt.NewRow();
                    rowTemp["ID"] = Index;
                    rowTemp["RawName"] = RAWName;
                    rowTemp["FoodTime"] = time.ToShortDateString();
                    //感官评分
                    E_ScoreData md_ScoreData = new E_ScoreData();
                    md_ScoreData.RawId = RawID;
                    md_ScoreData.ScoreTime = time;
                    List<E_ScoreData> eScoreData = t_ScoreData.GetDataEntityByTime(md_ScoreData);
                    if (eScoreData != null && eScoreData.Count > 0)
                    {
                        rowTemp["SenseScore"] = eScoreData.Sum(p => p.Score).ToString();
                        IsAdd = true;
                    }
                    else
                    {
                        rowTemp["SenseScore"] = "";
                    }

                    //检测
                    E_FoodRecord md = new E_FoodRecord();
                    md.RawID = RawID;
                    md.FoodTime = time;
                    for (int FoodType = 1; FoodType <= 15; FoodType++)
                    {
                        md.FoodTypeID = FoodType;//检测类别
                        E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md);
                        if (eFoodRecord != null)
                        {
                            IsAdd = true;
                        }
                        string DtColumnName = this.GetDtColumnName(FoodType);
                        rowTemp[DtColumnName] = (eFoodRecord != null) ? eFoodRecord.FoodResult : "";
                    }
                    if (IsAdd)
                    {
                        RawListDt.Rows.Add(rowTemp);
                        Index += 1;
                    }
                }
            }
            return RawListDt;
        }

        //导出检查数据列表
        protected void BtnExport_Click(object sender, EventArgs e)
        {
            string strRawIDList = this.HidRawList.Value;
            if (strRawIDList == "")
            {
                ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", "<script>alert('请寻找原料!');</script>");
                return;
            }
            DataTable FoodDt = this.GetFoodDt(strRawIDList);
            FoodDt.Columns["ID"].ColumnName = "序号";
            FoodDt.Columns["RawName"].ColumnName = "原料名称";
            FoodDt.Columns["FoodTime"].ColumnName = "检测时间";
            FoodDt.Columns["SenseScore"].ColumnName = "感官评分";
            FoodDt.Columns["VCFood"].ColumnName = "Vc含量";
            FoodDt.Columns["WaterFood"].ColumnName = "含水量(mg/100g)";
            FoodDt.Columns["CarbohydratesFood"].ColumnName = "总糖量";
            FoodDt.Columns["ProteinFood"].ColumnName = "蛋白质含量(g/100g)";
            FoodDt.Columns["MineralFood"].ColumnName = "锌含量(mg/100g)";
            FoodDt.Columns["VAFood"].ColumnName = "维生素A含量";
            FoodDt.Columns["VEFood"].ColumnName = "维生素E含量";
            FoodDt.Columns["AminoAcidFood"].ColumnName = "氨基酸含量";
            FoodDt.Columns["DietaryFiberFood"].ColumnName = "膳食纤维含量";
            FoodDt.Columns["CuFood"].ColumnName = "铜含量";
            FoodDt.Columns["FeFood"].ColumnName = "铁含量";
            FoodDt.Columns["ZnFood"].ColumnName = "锌含量";
            FoodDt.Columns["CaFood"].ColumnName = "钙含量";
            FoodDt.Columns["MgFood"].ColumnName = "镁含量";
            FoodDt.Columns["PFood"].ColumnName = "磷含量";

            ExcelRender.RenderToExcel(FoodDt, HttpContext.Current, Guid.NewGuid().ToString() + ".xls");
        }

        //根据检测类型获取列名称
        private string GetDtColumnName(int FoodTypeID)
        { 
            string ColumnName="";
            switch (FoodTypeID)
            { 
                case 1:ColumnName = "VCFood"; break;
                case 2: ColumnName = "WaterFood";break;
                case 3: ColumnName = "CarbohydratesFood";break;
                case 4: ColumnName = "ProteinFood";break;
                case 5:ColumnName = "MineralFood"; break;
                case 6: ColumnName = "VAFood"; break;
                case 7: ColumnName = "VEFood"; break;
                case 8: ColumnName = "AminoAcidFood"; break;
                case 9: ColumnName = "DietaryFiberFood"; break;
                case 10: ColumnName = "CuFood"; break;
                case 11: ColumnName = "FeFood"; break;
                case 12: ColumnName = "ZnFood"; break;
                case 13: ColumnName = "CaFood"; break;
                case 14: ColumnName = "MgFood"; break;
                case 15: ColumnName = "PFood"; break;
            }
            return ColumnName;
        }


    }
}